const express = require('express')
const next = require('next')

const port = parseInt(process.env.PORT, 10) || 3888
const dev = process.env.NODE_ENV !== 'production'
console.log(process.env.NODE_ENV)
console.log({dev})
const app = next({dev})
const handle = app.getRequestHandler()


app.prepare()
    .then(() => {
        const server = express()
        /*代理開啟*/
        var proxy = require('http-proxy-middleware');
        server.use('/DYApi', proxy({
            target: "http://192.168.8.194:8080",
            changeOrigin: true
        }));
        server.use('/mock', proxy({
            target: 'http://123.207.56.19:8946',
            changeOrigin: true
        }));

        /*路由控制*/
        server.get('/setting/design', (req, res) => {
            return app.render(req, res, '/settingDesignList', req.query)
        })
        server.get('/setting/design/add', (req, res) => {
            return app.render(req, res, '/settingDesignAdd', req.query)
        })
        server.get('/setting/design/add/design', (req, res) => {
            return app.render(req, res, '/settingDesignAddDesign', req.query)
        })
        server.get('/setting/order-history/:id', (req, res) => {
            return app.render(req, res, '/settingOrderHistoryDetail', req.query)
        })
        server.get('/setting/shop-settings', (req, res) => {
            return app.render(req, res, '/settingShopSettings', req.query)
        })
        server.get('/login', (req, res) => {
            return app.render(req, res, '/login', req.query)
        })

        server.get('/styles', (req, res) => {
            return app.render(req, res, '/styles', req.query)
        })
        server.get('/design-items/:design_id', (req, res) => {
            return app.render(req, res, '/productDetail', req.query)
        })
        server.get('/best-sellers', (req, res) => {
            return app.render(req, res, '/bestSellers', req.query)
        })
        server.get('/new-arrivals', (req, res) => {
            return app.render(req, res, '/newArrivals', req.query)
        })
        server.get('/become-designer', (req, res) => {
            return app.render(req, res, '/becomeDesigner', req.query)
        })


        server.get('/', (req, res) => {
            return app.render(req, res, '/index', req.query)
        })


        server.get('*', (req, res) => {
            return handle(req, res)
        })

        server.listen(port, (err) => {
            if (err) throw err
            console.log(`> Ready on http://localhost:${port}`)
        })
    })